package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.OperationLogs;
import com.example.mall.dto.OperationLogsDto;
import com.example.mall.mapper.OperationLogsMapper;
import com.example.mall.query.OperationLogsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IOperationLogsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class OperationLogsServiceImpl extends ServiceImpl<OperationLogsMapper, OperationLogs> implements IOperationLogsService {

    @Autowired
    private OperationLogsMapper operationLogsMapper;

    @Override
    public PageDTO<OperationLogsDto> queryPage(OperationLogsQuery operationLogsQuery) {
        if (operationLogsQuery == null) {
            operationLogsQuery = new OperationLogsQuery();
        }
        Page<OperationLogs> page = operationLogsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OperationLogs> cartPage = lambdaQuery()
                .like(operationLogsQuery.getOperationId()!= null, OperationLogs::getOperationId, operationLogsQuery.getOperationId())
                .like(operationLogsQuery.getUserId()!= null, OperationLogs::getUserId, operationLogsQuery.getUserId())
                .like(operationLogsQuery.getOperationType()!= null, OperationLogs::getOperationType, operationLogsQuery.getOperationType())
                .like(operationLogsQuery.getOperationDetail()!= null, OperationLogs::getOperationDetail, operationLogsQuery.getOperationDetail())
                .like(operationLogsQuery.getIpAddress()!= null, OperationLogs::getIpAddress, operationLogsQuery.getIpAddress())
                .orderByDesc(OperationLogs::getCreatedDate)
                .page(page);
        return PageDTO.of(cartPage, OperationLogsDto.class);
    }

    @Override
    public void insert(OperationLogsDto operationLogsDto) {
        OperationLogs operationLogs = new OperationLogs();
        CopyUtils.copyProperties(operationLogsDto, operationLogs);
        operationLogs.setOperationId(CodeGenerateUtils.generateUniqueId());
        operationLogs.setCreatedDate(new Date());
        operationLogsMapper.insert(operationLogs);
    }

    @Override
    public void update(OperationLogsDto operationLogsDto) {
        AssertUtil.isNotNull(operationLogsDto.getOperationId(), "操作日志表id不能为空");
        OperationLogs operationLogs = operationLogsMapper.selectById(operationLogsDto.getOperationId());
        AssertUtil.isNotNull(operationLogs, "操作日志表不存在");
        CopyUtils.copyProperties(operationLogsDto, operationLogs);
        operationLogsMapper.updateById(operationLogs);
    }

    @Override
    public void delete(Long operationId) {
        AssertUtil.isNotNull(operationId, "操作日志表id不能为空");
        OperationLogs operationLogs = operationLogsMapper.selectById(operationId);
        AssertUtil.isNotNull(operationLogs, "操作日志表不存在");
        operationLogsMapper.deleteById(operationId);
    }

    @Override
    public void deleteBatch(List<Long> operationIds) {
        AssertUtil.isNotNull(operationIds, "操作日志表id不能为空");
        List<OperationLogs> operationLogs = operationLogsMapper.selectBatchIds(operationIds);
        AssertUtil.isNotNull(operationLogs, "操作日志表不存在");
        operationLogsMapper.deleteBatchIds(operationIds);
    }

    @Override
    public OperationLogs queryOperationLogsById(Long operationId) {
        AssertUtil.isNotNull(operationId, "操作日志表id不能为空");
        return operationLogsMapper.selectById(operationId);
    }

    @Override
    public List<OperationLogs> queryOperationLogsList() {
        return this.list();
    }
}
